﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using System.IO;
using EnvDTE;

namespace WizardExtension
{
    public class DAImplementationExtension : ExtensionBase, IWizard
    {
        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {

        }

        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {

        }

        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
            string interfaceName = projectItem.Name;
            if (interfaceName.Contains("."))
                interfaceName = interfaceName.Substring(0, interfaceName.IndexOf("."));

            bool found = false;

            foreach (ProjectItem pi in projectItem.ContainingProject.ProjectItems)
            {
                for (short i = 0; i < pi.FileCount; i++)
                {
                    string filePath = pi.FileNames[i];

                    if (filePath.EndsWith("Providers\\"))
                    {
                        found = true;

                        AddDAImplementationPropertyCode(filePath + "DataAccessAccessPoint.cs", interfaceName);
                    }
                }
            }

            if (!found)
                MessageBox.Show("Unable to find Access Point Class file!" + Environment.NewLine + "Please add the new Type properties manually.");
        }

        private void AddDAImplementationPropertyCode(string filePath, string className)
        {
            string interfaceName = "I" + className;
            string entityName = className;
            if (entityName.EndsWith("DA"))
                entityName = entityName.Substring(0, entityName.Length - 2);

            string fileContent = File.ReadAllText(filePath);
            string propertyContent = Environment.NewLine + "     public " + interfaceName + " " + className + Environment.NewLine +
                "     { " + Environment.NewLine +
                "get " +
                " { return GetSharedInstance<" + className + ", " + entityName + ">() as " + className + "; }" + Environment .NewLine +
                "     }" + Environment.NewLine;

            int bracesCount = fileContent.Count(ch => ch == '}');
            int c = 0;
            for (int i = 0; i < fileContent.Length; i++)
                if (fileContent[i] == '}')
                {
                    c++;

                    if (c == (bracesCount - 1))
                    {
                        fileContent = fileContent.Substring(0, i - 1) + propertyContent + Environment.NewLine + "    }" + Environment.NewLine + "}";
                    }
                }

            File.WriteAllText(filePath, fileContent);
        }

        public void RunFinished()
        {

        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            AddBaseNamespace(automationObject, replacementsDictionary);

            string className = replacementsDictionary["$rootname$"].Replace(".cs", string.Empty);
            string entityName = className;

            if (entityName.StartsWith("I"))
                entityName = entityName.Substring(1, entityName.Length - 1);

            if (entityName.EndsWith("DA"))
                entityName = entityName.Substring(0, entityName.Length - 2);

            string interfaceName = "I" + replacementsDictionary["$rootname$"].Replace(".cs", string.Empty);

            replacementsDictionary.Add("$safeentityname$", entityName);
            replacementsDictionary.Add("$safeinterfacename$", interfaceName);
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}

